Module 04
What can be included in the aes() function?
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>)
)
Variables (quantitative or categorical) can be mapped to the following aesthetics:
What’s happening here?
The pair plot above is an example of a facet plot. We can create a facet plot by adding the facet_wrap() or facet_grid() function to the ggplot object.
facet_grid() (1/3)facet_grid() (1/3)facet_grid() (3/3)~ operatorUsing the ~ operator to specify the rows and columns of the facet grid.
Can you explain the use of “.” in R? Specifically, as it’s used in ggplot2 for functions like facet_grid(. ~ species)
The dot (.) in R, especially as used in ggplot2 functions like facet_grid(), has a special meaning. Let me explain its use:
In ggplot2’s facet_grid():
In the context of facet_grid(. ~ species), the dot serves as a placeholder. It indicates that you don’t want to split the plot on the rows (left side of the tilde ~), but you do want to split it on the columns (right side of the tilde) based on the “species” variable.
facet_grid(. ~ species): Creates separate plots for each unique value in the “species” column, arranged horizontally.facet_grid(species ~ .): Would create separate plots for each unique value in the “species” column, arranged vertically.facet_grid(var1 ~ var2): Would create a grid of plots, with “var1” determining the rows and “var2” the columns.In other R contexts:
The dot can also be used in other situations in R:
%>% or |>), the dot represents the object being passed through the pipe.Chapter 9 9.4.1 Exercises. Questions 1 through 7
Chapter 9 9.3.1 Exercises, questions 1 through 4
Several geometric objects in ggplot2 carry out statistical transformations, examples include:
geom_histogram() calculates the count of observations in bins.geom_density() calculates the density of a variable.geom_bar() calculates the count of observations for each level of a categorical variable.geom_boxplot() calculates the median, quartiles, and outliers of a variable.geom_smooth() calculates a smoothed line (or fit) through the data.Figure 9.2 from R4DS 2e provides the following graphic:
For every geometric object geom_*, there is a corresponding statistical transformation stat_*.
Additional transformations can be called to override the default values. For instance, the geom_histogram() includes the following:
These are calculated by the ‘stat’ part of layers and can be accessed with delayed evaluation.
after_stat(count)
number of points in bin.
after_stat(density)
density of points in bin, scaled to integrate to 1.
after_stat(ncount)
count, scaled to a maximum of 1.
after_stat(ndensity)
density, scaled to a maximum of 1.
after_stat(width)
widths of bins.
machine |>
group_by(machine, time) |>
summarise(mean_diameter = mean(diameter),
sd_diameter = sd(diameter), ci_diameter = 1.96 * sd(diameter) / sqrt(n())) |>
ungroup() |>
mutate(machine_time = str_c(machine, time, sep = "_")) |>
ggplot() +
geom_point(aes(x = machine_time, y = mean_diameter, color = machine),
position = position_dodge(width = 0.5)) +
geom_errorbar(aes(x = machine_time, ymin = mean_diameter - ci_diameter, ymax = mean_diameter + ci_diameter),
width = 0.25, position = position_dodge(width = 0.5))Applied Statistical Techniques